import wepy from 'wepy'

export default class extends wepy.mixin {

  data = {
    // 地址详细信息
    addressInfo: null,
    // 已选商品信息
    isCheckedGoods: [],
  }

  onLoad() {
    // 获取地址信息
    this.addressInfo = wepy.getStorageSync('address') || null
    // 获取已选商品数据
    this.isCheckedGoods = this.$parent.globalData.cart.filter(x => {
      return x.isCheck === true
    })
  }

  methods = {
    // 选择地址
    async chooseAdress() {
      const res = await wepy.chooseAddress().catch(x => x)
      if (res.errMsg === 'chooseAddress:ok') {
        this.addressInfo = res
        // 将地址信息存储到本地当中
        wepy.setStorageSync('address', this.addressInfo)
        return wepy.showToast({
          title: '选择地址成功',
          icon: 'success'
        })
      } else {
        return wepy.getToast('选择地址失败')
      }
    },
    goLogin() {
      wepy.get('/users/wxlogin')
    }
  }

  computed = {
    // 判断是否有地址
    isHaveAdress() {
      if (this.addressInfo === null) {
        return false
      }
      return true
    }
  }
}
